﻿/************************************************************************************************
	File:			ChaosSingleton.hpp
	Description:	Singleton templet defination.

	Author:			sniperbat
	Version:		0.0.1
	CreateDate:		2007-5-1
	LastModify:		2007-5-1
************************************************************************************************/
#ifndef	__CHAOSSINGLETON_HPP
#define	__CHAOSSINGLETON_HPP


#include <cassert>

namespace Chaos
{
	template <typename T> class ChsSingleton
	{
	public:
						ChsSingleton( void );
						~ChsSingleton( void );
		static T&		GetSingleton( void );
		static T*		GetSingletonPtr( void );

	protected:
		static T*		ms_Singleton;

	};

	template <typename T>ChsSingleton<T>::ChsSingleton( void )
	{
		assert( !ms_Singleton );
		ms_Singleton = static_cast< T* >( this );
	}

	template <typename T>ChsSingleton<T>::~ChsSingleton()
	{
		assert( ms_Singleton );
		ms_Singleton = 0;
	}

	template <typename T>T& ChsSingleton<T>::GetSingleton( void )
	{
		assert( ms_Singleton );
		return ( *ms_Singleton );
	}

	template <typename T>T* ChsSingleton<T>::GetSingletonPtr( void )
	{
		assert( ms_Singleton );
		return ms_Singleton;
	}

	template <typename T>T* ChsSingleton<T>::ms_Singleton = 0;
}

#endif	//__TSINGLETON_HPP
